#include "mainwindowmenu.h"
#include "ui_mainwindowmenu.h"

#include <QTabWidget>
#include <QTableWidgetItem>

MainWindowMenu::MainWindowMenu(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindowMenu)
{
    ui->setupUi(this);

    //设置窗口名和窗口大小
    setWindowTitle("海底捞点单系统 V1.7.9");
    setFixedSize(800, 480);

    //当有数据接受的时候，tcpSocket 会自动触发 readyRead 信号,在对应的槽函数中获取数据
    connect(tcpSocket, &QTcpSocket::readyRead, this, &MainWindowMenu::onRecvMenuDataSlot);

    //初始化
    //设置每一页的标题
    ui->tabWidget->setTabText(0, "锅底");
    ui->tabWidget->setTabText(1, "荤菜");
    ui->tabWidget->setTabText(2, "素菜");
    ui->tabWidget->setTabText(3, "主食");
    ui->tabWidget->setTabText(4, "饮料");

    //给菜单初始化
    QString name[DISHES_NUM] = {"酸汤锅底", "菌汤锅底", "牛油锅底", "骨茶锅底", "麻辣锅底",
                       "牛肉卷", "羊肉卷", "毛肚", "小酥肉", "虾滑",
                       "豆腐", "红苋菜", "生菜", "香菇", "玉米",
                       "米饭", "面条",
                       "酸梅汁", "柠檬汁"};

    int price[DISHES_NUM] = {38, 48, 48, 48, 38,
                     38, 28, 35, 18, 28,
                     8, 6, 6, 10, 6,
                     3, 4,
                     10, 8};

    for(int i = 0; i < DISHES_NUM; i ++)
    {
        info.dishName[i] = name[i];
        info.dishPrice[i] = price[i];
    }
}

MainWindowMenu::~MainWindowMenu()
{
    delete ui;
}

void MainWindowMenu::recvInfo(QString deskNum, QString peopleNum)
{
    //设置桌号和人数
    ui->lineEdit_desk_num->setText(deskNum);
    ui->lineEdit_people_num->setText(peopleNum);
}

void MainWindowMenu::on_btn_back_clicked()
{
    //因为在跳转时，记录了上一个界面
    //所以可以利用此函数获取当前窗口的父窗口对象指针
    QWidget *parent = this->parentWidget();
    parent->show();//把之前父界面的隐藏状态改为显示状态，显示父界面
    this->close();//关闭当前界面
    //销毁子界面
    delete this;
}

void MainWindowMenu::onRecvMenuDataSlot()
{
    //获取信号的发送者 ，也就是先确定到底是哪个客户端 发送数据
//    QTcpSocket *client = new
//    QTcpSocket *client =  (QTcpSocket*)this->sender();

//    QString data = tcpSocket->readAll();

//    QStringList list = data.split(':');
//    ui->lineEdit_desk_num->setText(list[0]);
//    ui->lineEdit_people_num->setText(list[1]);
}

//提交
void MainWindowMenu::on_btn_submit_clicked()
{
    QDialog *dialog = new QDialog(this);
    dialog->setFixedSize(400, 400);
    dialog->setAttribute(Qt::WA_DeleteOnClose); // 确保对话框关闭时自动删除
    dialog->setWindowTitle("提示！");

    QPushButton* closeButton = new QPushButton("关闭", dialog);
    connect(closeButton, &QPushButton::clicked, dialog, &QDialog::accept);

    QTextBrowser *dishesText = new QTextBrowser(dialog);
    dishesText->move(0, 50);
    dishesText->setFixedSize(400, 400);
    dishesText->setStyleSheet("QTextBrowser { font-size: 20px; }");
    dishesText->append(ui->lineEdit_desk_num->text() + " 号桌");

    int val[DISHES_NUM] = {ui->spinBox0->value(), ui->spinBox1->value(), ui->spinBox2->value(), ui->spinBox3->value(), ui->spinBox4->value(),
                           ui->spinBox5->value(), ui->spinBox6->value(), ui->spinBox7->value(), ui->spinBox8->value(), ui->spinBox9->value(),
                           ui->spinBox10->value(), ui->spinBox11->value(), ui->spinBox12->value(), ui->spinBox13->value(), ui->spinBox14->value(),
                           ui->spinBox15->value(), ui->spinBox16->value(),
                           ui->spinBox17->value(), ui->spinBox18->value()};

    int i = 0;
    for(i = 0; i < DISHES_NUM; i ++)
    {
        info.dishNum[i] = val[i];
    }

    QString dishes;
    int total = 0;

    for(i = 0; i < DISHES_NUM; i ++)
    {
        if(val[i] != 0)
        {
            total += info.dishNum[i] * info.dishPrice[i];
            dishes = dishes.append("%1 * %2份，%3元; ").arg(info.dishName[i]).arg(info.dishNum[i]).arg(info.dishNum[i] * info.dishPrice[i]);
        }
    }

    dishes = dishes.append(" 总共 %1 元").arg(total);
    qDebug() << dishes;

    QStringList list = dishes.split(";");
    QString str = list[0];
    list[0] = QString(" %1").arg(str);

    for(i = 0; i < list.size(); i ++)
    {
        qDebug() <<list[i];
        dishesText->append(list[i]);
    }

    //拼接 提交的标识和桌号，发送给服务器
    QString strInfo = QString(":%1:%2").arg(ui->lineEdit_desk_num->text()).arg("dishes");
    dishes.append(strInfo);

    tcpSocket->write(dishes.toUtf8().data());

    dialog->exec(); // 阻塞直到对话框关闭
}

//呼叫
void MainWindowMenu::on_btn_call_clicked()
{
    QDialog *dialog = new QDialog(this);
    dialog->setFixedSize(400, 400);
    dialog->setAttribute(Qt::WA_DeleteOnClose); // 确保对话框关闭时自动删除
    dialog->setWindowTitle("提示！");

    QPushButton* closeButton = new QPushButton("关闭", dialog);
    closeButton->setStyleSheet("QPushButton {"
                            "font: 20pt '微软雅黑';"  // 设置字体和大小
                            "color: rgb(0, 0, 0);"      // 设置文本颜色
                            "background-color: rgb(175, 175, 175);"
                            "border-radius: 5px;"      // 设置圆角
                            "}");
    connect(closeButton, &QPushButton::clicked, dialog, &QDialog::accept);

    button1 = new QPushButton("加一壶水", dialog);
    button1->setFixedSize(370, 50);
//    button1->setStyleSheet("QPushButton{font: 20pt '微软雅黑';color: rgb(0, 0, 0);border-radius:5px;}");
    button1->setStyleSheet("QPushButton {"
                            "font: 20pt '微软雅黑';"  // 设置字体和大小
                            "color: rgb(0, 0, 0);"      // 设置文本颜色
                            "background-color: rgb(175, 175, 175);"
                            "border-radius: 5px;"      // 设置圆角
                            "}");
    connect(button1, &QPushButton::clicked, this, &MainWindowMenu::sendInfoSlots2);

    button2 = new QPushButton("加一碗米饭", dialog);
    button2->setFixedSize(370, 50);
    button2->setStyleSheet("QPushButton {"
                            "font: 20pt '微软雅黑';"  // 设置字体和大小
                            "color: rgb(0, 0, 0);"      // 设置文本颜色
                            "background-color: rgb(175, 175, 175);"
                            "border-radius: 5px;"      // 设置圆角
                            "}");
    connect(button2, &QPushButton::clicked, this, &MainWindowMenu::sendInfoSlots2);

    button3 = new QPushButton("加一包纸巾", dialog);
    button3->setFixedSize(370, 50);
    button3->setStyleSheet("QPushButton {"
                            "font: 20pt '微软雅黑';"  // 设置字体和大小
                            "color: rgb(0, 0, 0);"      // 设置文本颜色
                            "background-color: rgb(175, 175, 175);"
                            "border-radius: 5px;"      // 设置圆角
                            "}");
    connect(button3, &QPushButton::clicked, this, &MainWindowMenu::sendInfoSlots2);

    button4 = new QPushButton("加一个碗和一双筷子", dialog);
    button4->setFixedSize(370, 50);
    button4->setStyleSheet("QPushButton {"
                            "font: 20pt '微软雅黑';"  // 设置字体和大小
                            "color: rgb(0, 0, 0);"      // 设置文本颜色
                            "background-color: rgb(175, 175, 175);"
                            "border-radius: 5px;"      // 设置圆角
                            "}");
    connect(button4, &QPushButton::clicked, this, &MainWindowMenu::sendInfoSlots2);

    QVBoxLayout* layout = new QVBoxLayout(dialog);
    layout->addWidget(button1);
    layout->addWidget(button2);
    layout->addWidget(button3);
    layout->addWidget(button4);
    layout->addWidget(closeButton);

    dialog->setLayout(layout);

    dialog->exec(); // 阻塞直到对话框关闭
}

void MainWindowMenu::sendInfoSlots2()
{
    if(sender() == button1)
        usrInfo = button1->text();
    else if(sender() == button2)
        usrInfo = button2->text();
    else if(sender() == button3)
        usrInfo = button3->text();
    else if(sender() == button4)
        usrInfo = button4->text();

    QString deskNum = ui->lineEdit_desk_num->text();
    QString info = QString("%1:%2:%3").arg(deskNum).arg(usrInfo).arg("call");
    //qDebug() << info;
    tcpSocket->write(info.toUtf8().data());
}

//支付
void MainWindowMenu::on_btn_pay_clicked()
{
    QDialog *dialog = new QDialog(this);
    dialog->setFixedSize(400, 400);
    dialog->setAttribute(Qt::WA_DeleteOnClose); // 确保对话框关闭时自动删除
    dialog->setWindowTitle("提示！");

    QPushButton* closeButton = new QPushButton("关闭", dialog);
    closeButton->setStyleSheet("QPushButton {"
                            "font: 20pt '微软雅黑';"  // 设置字体和大小
                            "color: rgb(0, 0, 0);"      // 设置文本颜色
                            "background-color: rgb(175, 175, 175);"
                            "border-radius: 20px;"      // 设置圆角
                            "}");
    connect(closeButton, &QPushButton::clicked, dialog, &QDialog::accept);
    closeButton->move(0, 220);
    closeButton->setFixedSize(400, 180);

    button5 = new QPushButton("扫码支付", dialog);
    button5->setStyleSheet("QPushButton {"
                            "font: 20pt '微软雅黑';"  // 设置字体和大小
                            "color: rgb(0, 0, 0);"      // 设置文本颜色
                            "background-color: rgb(175, 175, 175);"
                            "border-radius: 20px;"      // 设置圆角
                            "}");
    button5->move(0, 0);
    button5->setFixedSize(400, 180);
    connect(button5, &QPushButton::clicked, this, &MainWindowMenu::paySlots1);

    dialog->exec(); // 阻塞直到对话框关闭
}

void MainWindowMenu::sendPayInfo()
{
    QString str = QString("%1 号桌已支付:%2").arg(ui->lineEdit_desk_num->text()).arg("pay");
    qDebug() << str;
    tcpSocket->write(str.toUtf8().data());
}

//扫码支付
void MainWindowMenu::paySlots1()
{
    QDialog *dialog = new QDialog(this);
    dialog->setFixedSize(400, 400);
    dialog->setAttribute(Qt::WA_DeleteOnClose); // 确保对话框关闭时自动删除
    dialog->setWindowTitle("请扫描二维码！");

    QPushButton* closeButton = new QPushButton(dialog);
    closeButton->setStyleSheet("QPushButton {"  // 设置字体和大小
                            "border-radius: 10px;"      // 设置圆角
                            "}");
    connect(closeButton, &QPushButton::clicked, dialog, &QDialog::accept);
    connect(closeButton, &QPushButton::clicked, this, &MainWindowMenu::sendPayInfo);

    closeButton->move(0, 0);
    closeButton->setFixedSize(400, 400);

    closeButton->setIcon(QIcon(":/image/pay.jpg"));
    closeButton->setIconSize(QSize(closeButton->width(), closeButton->height()));

    dialog->exec(); // 阻塞直到对话框关闭
}

void MainWindowMenu::on_pushButton_3_clicked()
{
    QDialog *dialog = new QDialog(this);

    dialog->setWindowFlags(dialog->windowFlags() & ~Qt::FramelessWindowHint);
    dialog->setFixedSize(400, 400);
    dialog->setAttribute(Qt::WA_DeleteOnClose); // 确保对话框关闭时自动删除
    dialog->setWindowTitle("图片");

    QPushButton* imageButton = new QPushButton("", dialog);
    imageButton->setStyleSheet("QPushButton {"  // 设置字体和大小
                            "border-radius: 5px;"      // 设置圆角
                            "}");
    imageButton->setFixedSize(dialog->width(), dialog->height());

    imageButton->setIcon(QIcon(":/image/guodi1.jpg"));
    imageButton->setIconSize(QSize(imageButton->width(), imageButton->height()));

    imageButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    connect(imageButton, &QPushButton::clicked, dialog, &QDialog::accept);

    dialog->exec(); // 阻塞直到对话框关闭
}

void MainWindowMenu::on_pushButton_8_clicked()
{
    QDialog *dialog = new QDialog(this);

    dialog->setWindowFlags(dialog->windowFlags() & ~Qt::FramelessWindowHint);
    dialog->setFixedSize(400, 400);
    dialog->setAttribute(Qt::WA_DeleteOnClose); // 确保对话框关闭时自动删除
    dialog->setWindowTitle("图片");

    QPushButton* imageButton = new QPushButton("", dialog);
    imageButton->setStyleSheet("QPushButton {"  // 设置字体和大小
                            "border-radius: 5px;"      // 设置圆角
                            "}");
    imageButton->setFixedSize(dialog->width(), dialog->height());

    imageButton->setIcon(QIcon(":/image/guodi2.jpg"));
    imageButton->setIconSize(QSize(imageButton->width(), imageButton->height()));

    imageButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    connect(imageButton, &QPushButton::clicked, dialog, &QDialog::accept);

    dialog->exec(); // 阻塞直到对话框关闭
}

void MainWindowMenu::on_pushButton_9_clicked()
{
    QDialog *dialog = new QDialog(this);

    dialog->setWindowFlags(dialog->windowFlags() & ~Qt::FramelessWindowHint);
    dialog->setFixedSize(400, 400);
    dialog->setAttribute(Qt::WA_DeleteOnClose); // 确保对话框关闭时自动删除
    dialog->setWindowTitle("图片");

    QPushButton* imageButton = new QPushButton("", dialog);
    imageButton->setStyleSheet("QPushButton {"  // 设置字体和大小
                            "border-radius: 5px;"      // 设置圆角
                            "}");
    imageButton->setFixedSize(dialog->width(), dialog->height());

    imageButton->setIcon(QIcon(":/image/guodi3.jpg"));
    imageButton->setIconSize(QSize(imageButton->width(), imageButton->height()));

    imageButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    connect(imageButton, &QPushButton::clicked, dialog, &QDialog::accept);

    dialog->exec(); // 阻塞直到对话框关闭
}

void MainWindowMenu::on_pushButton_13_clicked()
{
    QDialog *dialog = new QDialog(this);

    dialog->setWindowFlags(dialog->windowFlags() & ~Qt::FramelessWindowHint);
    dialog->setFixedSize(400, 400);
    dialog->setAttribute(Qt::WA_DeleteOnClose); // 确保对话框关闭时自动删除
    dialog->setWindowTitle("图片");

    QPushButton* imageButton = new QPushButton("", dialog);
    imageButton->setStyleSheet("QPushButton {"  // 设置字体和大小
                            "border-radius: 5px;"      // 设置圆角
                            "}");
    imageButton->setFixedSize(dialog->width(), dialog->height());

    imageButton->setIcon(QIcon(":/image/guodi4.jpg"));
    imageButton->setIconSize(QSize(imageButton->width(), imageButton->height()));

    imageButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    connect(imageButton, &QPushButton::clicked, dialog, &QDialog::accept);

    dialog->exec(); // 阻塞直到对话框关闭
}

void MainWindowMenu::on_pushButton_14_clicked()
{
    QDialog *dialog = new QDialog(this);

    dialog->setWindowFlags(dialog->windowFlags() & ~Qt::FramelessWindowHint);
    dialog->setFixedSize(400, 400);
    dialog->setAttribute(Qt::WA_DeleteOnClose); // 确保对话框关闭时自动删除
    dialog->setWindowTitle("图片");

    QPushButton* imageButton = new QPushButton("", dialog);
    imageButton->setStyleSheet("QPushButton {"  // 设置字体和大小
                            "border-radius: 5px;"      // 设置圆角
                            "}");
    imageButton->setFixedSize(dialog->width(), dialog->height());

    imageButton->setIcon(QIcon(":/image/guodi5.jpg"));
    imageButton->setIconSize(QSize(imageButton->width(), imageButton->height()));

    imageButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    connect(imageButton, &QPushButton::clicked, dialog, &QDialog::accept);

    dialog->exec(); // 阻塞直到对话框关闭
}
